package com.example.nfc.barcodescan.decode;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;

import com.dtr.zbar.build.ZBarDecoder;
import com.example.nfc.barcodescan.android.CaptureActivity;
import com.example.nfc.barcodescan.android.ScanResult;
import com.example.nfc.barcodescan.bean.Result;
import com.example.nfc.barcodescan.common.Constant;

public final class DecodeHandler extends Handler {

    private static final String TAG = DecodeHandler.class.getSimpleName();


    private final CaptureActivity activity;
    private boolean running = true;
    private ZBarDecoder zBarDecoder;
    DecodeHandler(CaptureActivity activity) {
        try {
            if(CaptureActivity.isSupportScan) {
                zBarDecoder = new ZBarDecoder();
            }
        }catch (Exception e){

        }

        this.activity = activity;
    }

    @Override
    public void handleMessage(Message message) {
        if (!running) {
            return;
        }
        switch (message.what) {
            case Constant.DECODE:

                decode((byte[]) message.obj, message.arg1, message.arg2);
                break;
            case Constant.QUIT:
                running = false;
                Looper.myLooper().quit();
                break;
        }
    }

    /**
     *
     * 解码
     */
    private void decode(byte[] data, int width, int height) {

        byte[] tempData = data ;

        Result rawResult = null;

        byte[] rotatedData = new byte[data.length];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                rotatedData[x * height + height - y - 1] = data[x + y * width];
            }
        }


        int tmp = width; // Here we are swapping, that's the difference to #11
        width = height;
        height = tmp;
        data = rotatedData;
        String result = null;

        //扫描框宽度
        int mWidth = (int) (width * 0.9);
        //扫描框高度
        int mHeight = width*2/3;

        /*水平居中  偏上显示*/
        int leftOffset = (width - mWidth) / 2;
        int topOffset = (height - mHeight) / 6;

        try {
            if(zBarDecoder!=null) {
                result = zBarDecoder.decodeCrop(data, width, height, leftOffset, topOffset, mWidth, mHeight);
            }else{
                result = "识别失败,您的设备可能不支持APP所依赖的底层识别插件";
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        rawResult = new Result(result,null,null,null);
        Handler handler = activity.getHandler();
        if(TextUtils.isEmpty(result)){
            if (handler != null) {
                Message message = Message.obtain(handler, Constant.DECODE_FAILED);
                message.sendToTarget();
            }
        }else{
            Message message = Message.obtain(handler,
                    Constant.DECODE_SUCCEEDED, new ScanResult(rawResult,tempData));
            message.sendToTarget();
        }
    }

}
